function [index] = sub2ind_ludo(array_dims, subs)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	% This function returns the index to access one single element from an array of unknown dimension.
	% The indexes (one for each dimensions) are passed in an array.
	% This goes around the issue of sub2ind, for which we need to know the number of indexes (=the size of the array).
	% Example use:
	% M = rand(6,4,8,2);
	% M(5,2,7,1)
	% array_dims = size(M);
	% subs = [5,2,7,1];
	% M(sub2ind_ludo(array_dims, subs))
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Inputs:
	% array_dims:	vector of integers (dimensions of array)
	% subs:			vector of integers (index for each dimension)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Outputs:
	% 
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	if length(array_dims) ~= length(subs) || any(subs > array_dims)
		error('Wrong input');
	end
	
	index = cumprod([1 array_dims(1:end-1)]) * (subs(:) - [0; ones(numel(subs)-1, 1)]);
end
